package com.bjpowernode.controller;
import com.bjpowernode.beans.*;
import com.bjpowernode.exception.MyDateException;
import com.bjpowernode.exception.MyLengthException;
import com.bjpowernode.services.ActivityServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.UUID;

@RestController
@RequestMapping("/Activity")
public class ActivityController {
    @Autowired
    ActivityServices activityServices;
    @RequestMapping("getAll.action")
    public Page getAll(Page page) {
        //调用业务，准备填充剩余三个属性
        activityServices.getAll(page);
        return page;
    }

    @RequestMapping("del.action")
    public HashMap<String, Object> del(@RequestBody String[] ids) throws  SQLException {
        HashMap<String, Object> map = new HashMap<>();
        try {
            //mybatis会自己报SQL异常
            int del = activityServices.del(ids);
            //SQL语法不会错误，只是影响行数的错误（比如删除的没有的ID）
            if (del > 0) {
                map.put("result", "success");
            } else {
                throw new SQLException("由于业务逻辑错误，则数据库删除影响行数异常");
            }
        } catch (Exception exception) {
            throw new SQLException("由于SQL语句错误，则我们需要再次抛出一个异常交给增强控制器，数据库删除影响行数异常");
        }
        return map;
    }


    @RequestMapping("add.action")
    public HashMap<String, Object> add(HttpServletRequest res,   Activity activity) throws SQLException, MyLengthException, MyDateException {

        UUID uuid = UUID.randomUUID();
        activity.setId(uuid.toString());











        HashMap<String, Object> map = new HashMap<>();

        if (activity.getName().length() > 50 || activity.getName().length()<1) {
            throw new MyLengthException("名称的长度1-50");
        }
        if (activity.getDescription().length() > 200 || activity.getDescription().length() <1) {
            throw new MyLengthException("描述的长度1-200");
        }
        //判断日期
        String startDate = activity.getStartDate();
        String endDate = activity.getEndDate();
        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate st=LocalDate.parse(startDate,dateTimeFormatter);
        LocalDate et=LocalDate.parse(endDate,dateTimeFormatter);
        if(st.isAfter(et)){
            throw new MyDateException("活动开始日期不能大于结束日期");
        }

        //活动创建者(填写User对象的LoginAct属性，也就是账号，姓名会重复的)
        HttpSession session = res.getSession();
        User user =(User) session.getAttribute("user");
        activity.setCreateBy(user.getLoginAct());
        //活动创建的时间
        LocalDateTime now=LocalDateTime.now();
        DateTimeFormatter dateTimeFormatterSSS=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowTime = dateTimeFormatterSSS.format(now);
        activity.setCreateTime(nowTime);
        try {
            int add = activityServices.add(activity);
            if (add > 0) {
                map.put("result", "success");
            } else {
                throw new SQLException("由于业务逻辑错误，则数据库添加影响行数异常");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            throw new SQLException("由于SQL语句错误，则我们需要再次抛出一个异常交给增强控制器，数据库添加影响行数异常");
        }
        return map;
    }


    @RequestMapping("editBefore.action")
    public Activity editBefore(String id) throws SQLException, MyLengthException {
        Activity activity = activityServices.get(id);
        return activity;
    }

    @RequestMapping("edit.action")
    public HashMap<String, Object> edit(HttpServletRequest res,Activity activity) throws SQLException, MyLengthException, MyDateException {

        HashMap<String, Object> map = new HashMap<>();

        if (activity.getName().length() > 50 || activity.getName().length()<1) {
            throw new MyLengthException("名称的长度1-50");
        }
        if (activity.getDescription().length() > 200 || activity.getDescription().length() <1) {
            throw new MyLengthException("描述的长度1-200");
        }

        //判断日期
        String startDate = activity.getStartDate();
        String endDate = activity.getEndDate();
        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate st=LocalDate.parse(startDate,dateTimeFormatter);
        LocalDate et=LocalDate.parse(endDate,dateTimeFormatter);
        if(st.isAfter(et)){
            throw new MyDateException("活动开始日期不能大于结束日期");
        }
        //活动修改者(填写User对象的LoginAct属性，也就是账号，姓名会重复的)
        HttpSession session = res.getSession();
        User user =(User) session.getAttribute("user");
        activity.setEditBy(user.getLoginAct());
        //活动修改的时间
        LocalDateTime now=LocalDateTime.now();
        DateTimeFormatter dateTimeFormatterSSS=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowTime = dateTimeFormatterSSS.format(now);
        activity.setEditTime(nowTime);

        try {
            int edit = activityServices.edit(activity);
            if (edit > 0) {
                map.put("result", "success");
            } else {
                throw new SQLException("由于业务逻辑错误，则数据库修改影响行数异常");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            throw new SQLException("由于SQL语句错误，则我们需要再次抛出一个异常交给增强控制器，数据库修改影响行数异常");
        }
        return map;
    }

    @RequestMapping("getIdAndRemark.action")
    public Activity getIdAndRemark(String id) throws SQLException, MyLengthException {
        Activity activity = activityServices.getIdAndRemark(id);

        return activity;
    }

}
